import { useMemo } from 'react';
import { deepClone } from '~/public/cpas-business/tools/Tools';

export default function useCustomDataHook(
  dates: string[],
  type: 'year' | 'moon',
  isDetail: boolean,
) {
  /**
   * 表头数据
   */
  const tableColumnData = useMemo(
    () => generateTableColumnData(dates, type, isDetail),
    [dates, type, isDetail],
  );
  function generateTableColumnData(
    datesParam: string[],
    typeParam: string,
    isDetailParam: boolean,
  ): any[] {
    const sortDatesParam = deepClone(datesParam).sort((a: any, b: any) => a - b);

    // 基础信息列
    let columnBaseData: any[] = [
      // {
      //   caption: 'ID',
      //   dataField: 'typeID',
      //   width: 150,
      //   alignment: 'left',
      //   visible: false,
      // },
      {
        caption: '客户',
        dataField: 'custName',
        dataTypeCustom: 'zh-string',
        minWidth: 120,
        fixed: true,
        // width: 'auto',
        alignment: 'left',
      },
      {
        caption: '客户类型',
        dataField: 'typeName',
        width: 150,
        dataTypeCustom: 'zh-string',
        alignment: 'left',
      },
      {
        caption: '是否关联方',
        dataField: 'isRelate',
        width: 120,
        dataType: 'string',
        alignment: 'center',
        customizeText: (cellInfo: any) => {
          if (cellInfo.value === null) {
            return '';
          } else if (cellInfo.value === true) {
            return '是';
          } else {
            return '否';
          }
        },
      },
    ];

    if (isDetailParam) {
      const detailBaseColumn = [
        {
          caption: '客户',
          dataField: 'custName',
          dataTypeCustom: 'zh-string',
          minWidth: 120,
          fixed: true,
          // width: 'auto',
          alignment: 'left',
        },
        {
          caption: '商品名称',
          dataField: 'productName',
          dataTypeCustom: 'zh-string',
          minWidth: 120,
          fixed: true,
          alignment: 'left',
        },
        {
          caption: '客户类型',
          dataField: 'typeName',
          width: 150,
          dataTypeCustom: 'zh-string',
          alignment: 'left',
        },
        {
          caption: '商品类别',
          dataField: 'productType',
          width: 150,
          alignment: 'left',
          dataTypeCustom: 'zh-string',
        },

        {
          caption: '商品编码',
          dataField: 'productCode',
          width: 150,
          alignment: 'left',
        },

        {
          caption: '基本计量单位',
          dataField: 'baseUnitCode',
          dataTypeCustom: 'zh-string',
          width: 150,
          alignment: 'left',
        },
        {
          caption: '是否关联方',
          dataField: 'isRelate',
          width: 120,
          dataType: 'string',
          alignment: 'center',
          customizeText: (cellInfo: any) => {
            if (cellInfo.value === null) {
              return '';
            } else if (cellInfo.value === true) {
              return '是';
            } else {
              return '否';
            }
          },
        },
      ];
      columnBaseData = [...detailBaseColumn];
    }

    // 年度统计列
    const yearColumn: any = {
      caption: '本期期末',
      alignment: 'center',
      columns: [
        {
          caption: '数量',
          dataField: '',
          dataType: 'number',
          // dataTypeCustom: 'money-number',
          width: 120,
          alignment: 'right',
          customizeText: (cellInfo: any) => {
            const value = cellInfo?.value;
            if (value && +value !== 0) {
              return value.toString();
            } else {
              return '';
            }
          },
        },
        {
          caption: '金额',
          dataField: '',
          dataType: 'number',
          dataTypeCustom: 'money-number',
          width: 120,
          alignment: 'right',
        },
      ],
    };

    if (isDetailParam) {
      yearColumn.columns.push({
        caption: '均价',
        dataField: '',
        width: 120,
        dataType: 'number',
        dataTypeCustom: 'money-number',
        alignment: 'right',
      });
    } else {
      yearColumn.columns.push({
        caption: '占比',
        dataField: '',
        dataType: 'number',
        format: (cellInfo: any) => {
          if (cellInfo === 0) {
            return '';
          } else {
            return (cellInfo * 100).toFixed(2) + '%';
          }
        },
        width: 80,
        alignment: 'right',
      });
      yearColumn.columns.push({
        caption: '排名',
        dataField: 'OrderIndex',
        width: 80,
        dataType: 'number',
        alignment: 'right',
        customizeText: (cellInfo: any) => {
          const value = cellInfo?.value;
          if (value && +value !== 0) {
            return value.toString();
          } else {
            return '';
          }
        },
      });
    }

    // 月度统计列
    const moonColumn = {
      caption: '本期期末',
      alignment: 'center',
      columns: [
        {
          caption: '数量',
          dataField: '',
          width: 120,
          dataType: 'number',
          // dataTypeCustom: 'money-number',
          alignment: 'right',
          customizeText: (cellInfo: any) => {
            const value = cellInfo?.value;
            if (value && +value !== 0) {
              return value.toString();
            } else {
              return '';
            }
          },
        },
        {
          caption: '金额',
          dataField: '',
          width: 120,
          dataType: 'number',
          dataTypeCustom: 'money-number',
          alignment: 'right',
        },
      ],
    };

    if (isDetailParam) {
      moonColumn.columns.push({
        caption: '均价',
        dataField: '',
        width: 120,
        dataType: 'number',
        dataTypeCustom: 'money-number',
        alignment: 'right',
      });
    }

    const countColumn: any = [
      {
        caption: '总计',
        alignment: 'center',
        columns: [
          {
            caption: '数量',
            dataField: 'Qty',
            width: 120,
            dataType: 'number',
            // dataTypeCustom: 'money-number',
            alignment: 'right',
            customizeText: (cellInfo: any) => {
              const value = cellInfo?.value;
              if (value && +value !== 0) {
                return value.toString();
              } else {
                return '';
              }
            },
          },
          {
            caption: '金额',
            dataField: 'cost',
            width: 120,
            dataType: 'number',
            dataTypeCustom: 'money-number',
            alignment: 'right',
          },
        ],
      },
    ];

    if (isDetailParam) {
      countColumn[0].columns.push({
        caption: '均价',
        dataField: 'Avg',
        width: 120,
        dataType: 'number',
        dataTypeCustom: 'money-number',
        alignment: 'right',
      });
    } else {
      countColumn[0].columns.push(
        {
          caption: '占比',
          dataField: 'Rate',
          width: 80,
          dataType: 'number',
          dataTypeCustom: 'money-number',
          alignment: 'right',
          customizeText: (cellInfo: any) => {
            if (cellInfo.value === 0) {
              return '';
            } else {
              return (cellInfo.value * 100).toFixed(2) + '%';
            }
          },
        },
        {
          caption: '排名',
          dataField: 'OrderIndex',
          width: 80,
          dataType: 'number',
          alignment: 'right',
          customizeText: (cellInfo: any) => {
            const value = cellInfo?.value;
            if (value && +value !== 0) {
              return value.toString();
            } else {
              return '';
            }
          },
        },
      );
    }

    const dynamicColumns: any[] = [];

    sortDatesParam.forEach((dateItem: any) => {
      if (typeParam === 'year') {
        const newYearColumn: any = deepClone(yearColumn);

        newYearColumn.caption = dateItem + '年';
        // 数量
        newYearColumn.columns[0].dataField = `${dateItem}_Qty`;
        // 金额
        newYearColumn.columns[1].dataField = `${dateItem}_cost`;

        if (isDetailParam) {
          // 均价
          newYearColumn.columns[2].dataField = `${dateItem}_Avg`;
        } else {
          // 占比
          newYearColumn.columns[2].dataField = `${dateItem}_Rate`;
          newYearColumn.columns[3].dataField = `${dateItem}_OrderIndex`;
        }

        dynamicColumns.push(newYearColumn);
      } else {
        const newMoonColumn: any = deepClone(moonColumn);

        newMoonColumn.caption = dateItem + '月';
        // 数量
        newMoonColumn.columns[0].dataField = `${dateItem}_Qty`;
        if (isDetailParam) {
          // 金额
          newMoonColumn.columns[1].dataField = `${dateItem}_cost`;
          // 均价
          newMoonColumn.columns[2].dataField = `${dateItem}_Avg`;
        } else {
          // 金额
          newMoonColumn.columns[1].dataField = `${dateItem}_cost`;
        }

        dynamicColumns.push(newMoonColumn);
      }
    });

    if (type === 'year') {
      return [...columnBaseData, ...dynamicColumns];
    } else {
      return [...columnBaseData, ...dynamicColumns, ...countColumn];
    }
  }

  return {
    tableColumnData,
  };
}
